package com.bckj.fastboot.iot.parse.service.impl;

import com.bckj.fastboot.iot.common.constant.IotMQConstants;
import com.bckj.fastboot.iot.common.dto.msg.IotAlarmMessage;
import com.bckj.fastboot.iot.common.dto.query.IotAlarmDataQuery;
import com.bckj.fastboot.iot.common.dto.request.IotAlarmDataRequest;
import com.bckj.fastboot.iot.common.dto.response.IotAlarmDataResponse;
import com.bckj.fastboot.iot.common.mq.producer.IotAlarmMessageProducer;
import com.bckj.fastboot.iot.common.repository.IotAlarmDataRepository;
import com.bckj.fastboot.iot.parse.service.IotAlarmDataService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Set;

/**
 * IotAlarmDataHandler
 *
 * @author wfc
 **/
@Slf4j
@Service
public class IotAlarmDataServiceImpl implements IotAlarmDataService {
    @Resource
    private IotAlarmMessageProducer alarmInfoMessageProducer;
    @Resource
    private IotAlarmDataRepository iotAlarmDataRepository;

    public void handle(IotAlarmDataRequest request) {
        String devCode = request.getDevCode();
        // 报警产生恢复逻辑
        Map<String, Object> dataMap = request.getDataMap();
        Set<Map.Entry<String, Object>> entries = dataMap.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value == null) {
                log.warn("alarm value is null, key: {}", key);
                continue;
            }
            var query = new IotAlarmDataQuery();
            query.setDevCode(devCode);
            query.setAlarmCode(key);
            query.setAlarmStatus(1);
            IotAlarmDataResponse alarmData = iotAlarmDataRepository.findLatestOne(query);
            if (isAlarm(value)) {
                // 判断是否已经报警
                if (alarmData == null || alarmData.getAlarmStatus() == 0) {
                    var ins = baseCopy(request);
                    ins.setAlarmCode(key);
                    ins.setAlarmStatus(1);
                    boolean r = iotAlarmDataRepository.save(ins);
                    if (r) {
                        // 发送报警信息
                        sendAlarmInfoMessage(devCode, key);
                    }
                } else {
                    var upd = new IotAlarmDataRequest();
                    upd.setId(alarmData.getId());
                    upd.setUpdateTime(LocalDateTime.now());
                    iotAlarmDataRepository.updateById(upd);
                }
            } else {
                // 恢复逻辑
                if (alarmData != null) {
                    var upd = new IotAlarmDataRequest();
                    upd.setId(alarmData.getId());
                    upd.setAlarmStatus(0);
                    upd.setUpdateTime(LocalDateTime.now());
                    iotAlarmDataRepository.updateById(upd);
                }
            }
        }
    }

    private IotAlarmDataRequest baseCopy(IotAlarmDataRequest request) {
        var req = new IotAlarmDataRequest();
        req.setDevCode(request.getDevCode());

        return req;
    }

    private boolean isAlarm(Object value) {
        if (value instanceof Boolean bool) {
            return bool;
        }
        if (value instanceof Number num) {
            return num.intValue() > 0;
        }
        return false;
    }

    private void sendAlarmInfoMessage(
            String devCode,
            String alarmCode
    ) {
        var message = new IotAlarmMessage();
        message.setDevCode(devCode);
        message.setAlarmCode(alarmCode);
        message.setTag(IotMQConstants.IOT_ALARM_NOTICE_TAG);
        alarmInfoMessageProducer.sendAsync(message);
    }

    private void setIotDeviceAlarm(String devCode, String alarmCode, boolean isAlarm) {
        if (isAlarm) {
            // 添加报警
            if (StringUtils.hasText(alarmCode)) {

            }
        } else {
            // 恢复报警

        }
    }
}
